Micron Document
Livres et Wikis | Archives | Info


Java syntax
part 11/23 Β· 86.1 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
To make a code block synchronized, it is preceded by the synchronized keyword followed by the lock object inside the brackets. When the executing thread reaches the synchronized block, it acquires a mutual exclusion lock, executes the block, then releases the lock. No threads may enter this block until the lock is released. Any non-null reference type may be used as the lock.

/* Acquires lock on someObject. It must be of
a reference type and must be non-null */
synchronized (someObject) {
// Synchronized statements
}

assert statement

assert statements have been available since J2SE 1.4. These types of statements are used to make assertions in the source code, which can be turned on and off during execution for specific classes or packages. To declare an assertion the assert keyword is used followed by a conditional expression. If it evaluates to false when the statement is executed, an exception is thrown. This statement can include a colon followed by another expression, which will act as the exception's detail message.

// If n equals 0, AssertionError is thrown
assert n != 0;
/* If n equals 0, AssertionError will be thrown
with the message after the colon */
assert n != 0 : "n was equal to zero";

Primitive types

Primitive types in Java include integer types, floating-point numbers, UTF-16 code units and a Boolean type. There are no unsigned types in Java except char type, which is used to represent UTF-16 code units. The lack of unsigned types is offset by introducing unsigned right shift operation (>>>), which is not present in C++. Nevertheless, criticisms have been leveled about the lack of compatibility with C and C++ this causes.cite-ref-3[3]

| Primitive Types | | |
|---|---|---|
| Type Name | Wrapper class | Value |
| byte | java.lang.Byte | integer |
| short | java.lang.Short | integer |
| int | java.lang.Integer | integer |
| long | java.lang.Long | integer |
| float | java.lang.Float | floating point number |
| double | java.lang.Double | floating point number |
| boolean | java.lang.Boolean | Boolean |
| char | java.lang.Character | UTF-16 code unit ( BMP character or a p… |

| Primitive Types | | | |
|---|---|---|---|
| Type Name | Range | Size | Default Value |
| byte | βˆ’128 through +127 | 8-bit (1-byte) | 0 |
| short | βˆ’32,768 through +32,767 | 16-bit (2-byte) | 0 |
| int | βˆ’2,147,483,648 through +2,147,483,647 | 32-bit (4-byte) | 0 |
| long | βˆ’9,223,372,036,854,775,808 through +9,2… | 64-bit (8-byte) | 0 |
| float | Β±1.401298Eβˆ’45 through Β±3.402823E+38 | 32-bit (4-byte) | 0.0f |
| double | Β±4.94065645841246Eβˆ’324 through Β±1.79769… | 64-bit (8-byte) | 0.0 |
| boolean | true or false | 1-bit (1-bit) | false |
| char | '\u0000' through '\uFFFF' | 16-bit (2-byte) | '\u0000' |

char does not necessarily correspond to a single character. It may represent a part of a surrogate pair, in which case Unicode code point is represented by a sequence of two char values.

Boxing and unboxing


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────